﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Snefru.XmlObjects;
using System.Windows.Forms;
using System.Reflection;

namespace Snefru.Managers
{
    class UtilityManager
    {
        public static bool ValidateUrl(string strUrl)
        {
            try
            {
                Uri address = new Uri(strUrl);
                if (!string.IsNullOrEmpty(strUrl) && !strUrl.Contains("[SQL]"))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch (UriFormatException)
            {
                return false;
            }
        }
        public static void LoadDatabaseInstancesCombo(ComboBox combobox)
        {
            List<DatabaseInstance> databaseInstance = new SnefruQuerySettings().GetDetail(SnefruQuerySettings.XmlPath).DatabaseInstances;
            combobox.DataSource = databaseInstance;
            combobox.DisplayMember = "Type";
            combobox.ValueMember = "ID";
        }
        public static void LoadInjectionQueriesCombo(ComboBox combobox, Int32 databaseInstancesID)
        {
            List<InjectionQuery> collection = new List<InjectionQuery>();
            InjectionQuery pleaseSelect = new InjectionQuery();
            pleaseSelect.Name = "Please Select";
            pleaseSelect.Query = string.Empty;
            collection.Add(pleaseSelect);

            SnefruQuerySettings settings = new SnefruQuerySettings().GetDetail(SnefruQuerySettings.XmlPath);
            for (int i = 0; i < settings.DatabaseInstances.Count; i++)
            {
                if (settings.DatabaseInstances[i].ID == databaseInstancesID)
                {
                    for (int j = 0; j < settings.DatabaseInstances[i].InjectionQueries.Count; j++)
                    {
                        collection.Add(settings.DatabaseInstances[i].InjectionQueries[j]);
                    }
                }
            }

            combobox.DataSource = collection;
            combobox.DisplayMember = "Name";
            combobox.ValueMember = "Query";
        }
        public static List<InjectionQuery> LoadTableQuery(Int32 databaseInstancesID, Int32 SelectedInjectionQueryID)
        {
            List<InjectionQuery> collection = new List<InjectionQuery>();
            SnefruQuerySettings settings = new SnefruQuerySettings().GetDetail(SnefruQuerySettings.XmlPath);
            for (int i = 0; i < settings.DatabaseInstances.Count; i++)
            {
                if (settings.DatabaseInstances[i].ID == databaseInstancesID)
                {
                    for (int j = 0; j < settings.DatabaseInstances[i].InjectionQueries.Count; j++)
                    {
                        if (settings.DatabaseInstances[i].InjectionQueries[j].InjectionQueryID == SelectedInjectionQueryID)
                        {
                            collection.Add(settings.DatabaseInstances[i].InjectionQueries[j]);    
                        }                        
                    }
                }
            }

            return collection;
        }
        public static string GetStringValueFromEnum(Enum value)
        {
            string output = null;
            Type type = value.GetType();

            FieldInfo fi = type.GetField(value.ToString());
            StringValue[] attrs =  fi.GetCustomAttributes(typeof(StringValue), false) as StringValue[];
            if (attrs.Length > 0)
            {
                output = attrs[0].Value;
            }
            return output;
        }
    }
    class StringValue : Attribute
    {
        private string _value;
        public StringValue(string value)
        {
            _value = value;
        }
        public string Value
        {
            get { return _value; }
        }
    }
}
